package com.ccy_blog.back.controller;

import com.ccy_blog.back.bean.*;
import com.ccy_blog.back.service.ArticleService;
import com.ccy_blog.back.service.NoticeService;
import com.ccy_blog.base.bean.ResultVo;
import com.ccy_blog.base.exception.BlogException;
import com.ccy_blog.base.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/article/list")
    @ResponseBody
    public PageInfo<Article> list(int page, int pageSize, String title, HttpSession session){
        //获取当前登录用户
        User user = (User) session.getAttribute("user");
        //参数1:当前页码 参数2:每页记录数 pageSize 该方法等同于 limit a,b
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleService.list(user.getUid(),title);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }
    //博客页面文章展示
    @RequestMapping("/article/allList")
    @ResponseBody
    public List<Article> allList(String tagNames,HttpServletRequest request, Model model){
        List<Article> articles = articleService.list("1",tagNames);
        return articles;
    }
    //具体文章展示页
    @RequestMapping("/article/read/{aid}")
    public String read(@PathVariable("aid") String aid, HttpServletRequest request, Model model){
        //获取当前的文章
        Article article = articleService.queryById(aid);
        model.addAttribute("article",article);
        return "/workbench/article/read";
    }
    //异步修改文章是否公开
    @RequestMapping("/article/isOpen")
    @ResponseBody
    public ResultVo isOpen(Article article){
        ResultVo resultVo = new ResultVo();
        try {
            articleService.isOpen(article);
            resultVo.setOk(true);
            if(article.getIsOpen().equals("0")){
                resultVo.setMess("文章已私密");
            }else{
                resultVo.setMess("文章已公开");
            }
        }catch (BlogException e){
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

    //异步查询所有栏目
    @RequestMapping("/article/queryCategory")
    @ResponseBody
    public List<Category> queryCategory(){
        List<Category> categories = articleService.queryCategory();
        return categories;
    }

    //查询栏目下的标签
    @RequestMapping("/article/queryTags")
    @ResponseBody
    public List<Tag> queryTags(String cid){
        List<Tag> tags = articleService.queryTags(cid);
        return tags;
    }

    //异步发布文章
    @RequestMapping("/article/saveOrUpdate")
    @ResponseBody
    public ResultVo saveOrUpdate(Article article,HttpSession session){
        ResultVo resultVo = new ResultVo();
        try {
            //获取登录用户
            User user = (User) session.getAttribute("user");
            article.setUid(user.getUid());
            article = articleService.saveOrUpdate(article);
            resultVo.setOk(true);
            if(article.getAid() == null){
                resultVo.setMess("发布文章成功");
            }else{
                resultVo.setMess("修改文章文章成功");
            }
            resultVo.setT(article);
        }catch (BlogException e){
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }


    //异步查询文章信息
    @RequestMapping("/article/queryById")
    @ResponseBody
    public Article queryById(String id){
       Article article = articleService.queryById(id);
       return article;
    }

    //异步删除文章
    @RequestMapping("/article/deleteById")
    @ResponseBody
    public ResultVo deleteById(String id){
        ResultVo resultVo = new ResultVo();
        try {
            articleService.deleteById(id);
            resultVo.setOk(true);
            resultVo.setMess("删除文章成功");
        }catch (BlogException e){
            resultVo.setMess(e.getMessage());
        }
        return resultVo;
    }

}
